package com.wang.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.auth.service.*;
import com.wang.common.result.Result;
import com.wang.model.process.Process;
import com.wang.model.process.ProcessTemplate;
import com.wang.model.process.ProcessType;
import com.wang.model.process.dto.ProcessDto;
import com.wang.model.process.vo.ApprovalVo;
import com.wang.model.process.vo.ProcessFormVo;
import com.wang.model.process.vo.ProcessQueryVo;
import com.wang.model.system.SysDept;
import com.wang.model.system.SysPost;
import com.wang.model.system.SysUser;
import com.wang.security.custom.LoginInfoHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Options;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 审批类型 前端控制器
 * </p>
 *
 * @author wang
 * @since 2023-04-17
 */
@Api(tags = "审批流管理")
@RestController
@RequestMapping("/admin/process")
@CrossOrigin    //支持跨域
public class ProcessController {
    @Autowired
    private ProcessService processService;

    @Autowired
    private ProcessTypeService processTypeService;

    @Autowired
    private ProcessTemplateService processTemplateService;

    //审批管理列表
    @ApiOperation(value = "获取分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page, @PathVariable Long limit, ProcessQueryVo processQueryVo) {
        Page<Process> page1 = new Page<>(page,limit);
        Page<ProcessDto> processDtoPagePage = processService.selectPage(page1,processQueryVo);
        return Result.success(processDtoPagePage);
    }

    //查询所有审批分类和每个分类中的所有审批模板
    @GetMapping("/findProcessType")
    public Result findProcessType(){
        List<ProcessType> list = processTypeService.findProcessType();
        return Result.success(list);
    }

    @GetMapping("/getProcessTemplate/{templateId}")
    public Result getProcessTemplate(@PathVariable("templateId") String id){
        ProcessTemplate processTemplate = processTemplateService.getById(id);
        return Result.success(processTemplate);
    }

    @PostMapping("/startUp")
    public Result startUp(@RequestBody ProcessFormVo processFormVo){
        return processService.startUp(processFormVo);
    }

    @ApiOperation(value = "待处理")
    @GetMapping("/findPending/{page}/{limit}")
    public Result findPending(
            @ApiParam(name = "page",value = "当前页码",required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit",value = "每页记录树",required = true)
            @PathVariable Long limit
    ){
        Page<Process> pageProcess = new Page<>(page,limit);
        Page<ProcessDto> pageModel = processService.findPending(pageProcess);
        return Result.success(pageModel);
    }

    //查看审批详情信息
    @GetMapping("/show/{id}")
    public Result show(@PathVariable("id") Long id){
        Map<String,Object> map = processService.show(id);
        return Result.success(map);
    }

    //审批
    @ApiOperation(value = "审批")
    @PostMapping("/approve")
    public Result approve(@RequestBody ApprovalVo approvalVo){
        processService.approve(approvalVo);
        return Result.success();
    }

    @ApiOperation(value = "已处理")
    @GetMapping("/findProcessed/{page}/{limit}")
    public Result findProcessed(@PathVariable Long limit, @PathVariable Long page){
        Page<Process> processPage = new Page<>(page,limit);
        IPage<ProcessDto> processDtoIPage = processService.findProcessed(processPage);
        return Result.success(processDtoIPage);
    }

    @ApiOperation(value = "已发起")
    @GetMapping("/findStarted/{page}/{limit}")
    public Result findStarted(@PathVariable Long page, @PathVariable Long limit){
        Page<Process> processPage = new Page<>(page,limit);
        IPage<ProcessDto> processDtoIPage = processService.findStarted(processPage);
        return Result.success(processDtoIPage);

    }


}
